import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

///选择日期
class SimpleSelectDate extends StatefulWidget {
  ///
  final ValueChanged<String> onCheckUpdate;

  ///
  const SimpleSelectDate({Key key, this.onCheckUpdate})
      : super(key: key); //确定回调
  @override
  _SimpleSelectDateState createState() => _SimpleSelectDateState();
}

class _SimpleSelectDateState extends State<SimpleSelectDate> {
  List<int> year = [];
  List<String> month = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10",
    "11",
    "12"
  ];
  FixedExtentScrollController _yearController =
      FixedExtentScrollController(initialItem: 0);
  FixedExtentScrollController _monthController =
      FixedExtentScrollController(initialItem: 0);
  int nextYearGroup;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    nextYearGroup = DateTime.now().year;
    for (int i = 0; i < 20; i++) {
      nextYearGroup -= 1;
      year.add(nextYearGroup);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: setWidth(345),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            margin: EdgeInsets.only(top: setWidth(40)),
            height: setWidth(176),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: CupertinoPicker(
                    scrollController: _yearController,
                    useMagnifier: true,
                    magnification: 1.1,
                    itemExtent: 44,
                    onSelectedItemChanged: (index) {
                      _monthController.jumpToItem(0);
                      if (year.length <= index + 5 &&
                          year[year.length - 1] > 1970) {
                        for (int i = 0; i < 20; i++) {
                          if ((nextYearGroup -= 1) >= 1970) {
                            year.add(nextYearGroup);
                          }
                        }
                        setState(() {});
                      }
                    },
                    children: year
                        .asMap()
                        .map((key, value) {
                          return MapEntry(
                              key,
                              Container(
                                alignment: Alignment.center,
                                child: Text(
                                  "$value",
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: setSp(20)),
                                ),
                              ));
                        })
                        .values
                        .toList(),
                  ),
                ),
                Expanded(
                  child: CupertinoPicker(
                    scrollController: _monthController,
                    useMagnifier: true,
                    magnification: 1.1,
                    itemExtent: 44,
                    onSelectedItemChanged: (index) {},
                    children: month
                        .asMap()
                        .map((key, value) {
                          return MapEntry(
                              key,
                              Container(
                                alignment: Alignment.center,
                                child: Text(
                                  value,
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: setSp(20)),
                                ),
                              ));
                        })
                        .values
                        .toList(),
                  ),
                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: ScreenUtil.bottomBarHeight + 35),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  padding: EdgeInsets.only(
                      left: setWidth(42),
                      top: setWidth(12),
                      bottom: setWidth(12),
                      right: setWidth(42)),
                  decoration: BoxDecoration(
                    color: Color(0xffF2F2F2),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Text(
                    "取消",
                    style: TextStyle(
                        color: Color(0xff010015), fontSize: setSp(15)),
                  ),
                ),
                SizedBox(
                  width: setWidth(30),
                ),
                GestureDetector(
                  onTap: () {
                    int y = year[_yearController.selectedItem];
                    String m = month[_monthController.selectedItem];
                    widget.onCheckUpdate("选中时间:$y$m");
                    Navigator.pop(context);
                  },
                  child: Container(
                    padding: EdgeInsets.only(
                        left: setWidth(42),
                        top: setWidth(12),
                        bottom: setWidth(12),
                        right: setWidth(42)),
                    decoration: BoxDecoration(
                      color: YTTheme.theme,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      "确定",
                      style:
                          TextStyle(color: Colors.white, fontSize: setSp(15)),
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
